/**
 * RDDL: Implements a random policy for a domain with a single boolean action.
 *       The action selected to be true is the first ground instance.
 * 
 * @author Scott Sanner (ssanner@gmail.com)
 * @version 10/10/10
 *
 **/

package br.usp.pcs.lti.planning;

import java.util.ArrayList;
import java.util.Map;
import java.util.Random;

import rddl.ActionGenerator;
import rddl.EvalException;
import rddl.RDDL.PVAR_INST_DEF;
import rddl.State;
import rddl.policy.Policy;

public class RandomPolicy extends Policy {

    public Random _rand = new Random();

    public RandomPolicy() {
        super();
    }

    public RandomPolicy(String instance_name) {
        super(instance_name);
    }

    public ArrayList<PVAR_INST_DEF> getActions(State s) throws EvalException {

        if (s == null) {
            return new ArrayList<PVAR_INST_DEF>();
        }

        // Get a map of { legal action names -> RDDL action definition }
        Map<String, ArrayList<PVAR_INST_DEF>> action_map = ActionGenerator.getLegalBoolActionMap(s);

        // Return a random action selection
        ArrayList<String> actions = new ArrayList<String>(action_map.keySet());
        String action_taken = actions.get(this._rand.nextInt(action_map.size()));
        // System.out.println("\n--> Action taken: " + action_taken);

        return action_map.get(action_taken);
    }
}
